//
// Created by changlei on 2022/4/26.
//

import Foundation
import UIKit


protocol Viewable: Codable, NSCopying {
    var cover: String { get }
    var isPlayable: Bool { get }
    var title: String { get }
    var url: String { get }
    var coverRate: Double { get }
}

extension Viewable {
    var contextMenuConfiguration: UIContextMenuConfiguration {
        buildContextConfiguration {
            ViewableChromeController(self, placeholder: ViewablePlaceholder(), isReadOnly: true)
        }
    }

    var barrenContextMenuConfiguration: UIContextMenuConfiguration {
        buildContextConfiguration()
    }

    private func buildContextConfiguration(_ previewProvider: UIContextMenuContentPreviewProvider? = nil) -> UIContextMenuConfiguration {
        UIContextMenuConfiguration(identifier: self, previewProvider: previewProvider) { elements in
            let browser = UIAction(title: "打开链接", image: UIImage(systemName: "safari")) { action in
                Modules.openBrowser(self.url)
            }
            let readingList = UIAction(title: "添加到阅读列表", image: UIImage(systemName: "eyeglasses")) { action in
                Modules.saveToReadingList(self.url)
            }
            let copy = UIAction(title: "拷贝链接", image: UIImage(systemName: "doc.on.doc")) { action in
                Modules.copy(self.url)
            }
            let share = UIAction(title: "共享…", image: UIImage(systemName: "square.and.arrow.up")) { action in
                Modules.share(self.url)
            }
            let photo = UIAction(title: "添加到“照片”", image: UIImage(systemName: "square.and.arrow.down")) { action in
                Modules.saveToPhoto(self.cover)
            }
            return UIMenu(children: [browser, readingList, copy, share, photo])
        }
    }

    var detailController: UIViewController {
        ViewableChromeController(self)
    }

    var leadingSwipeActionsConfiguration: UISwipeActionsConfiguration {
        let fabulous = UIContextualAction(style: .normal, title: "想看") { action, view, closure in
            closure(true)
        }
        fabulous.backgroundColor = .systemGreen
        let follow = UIContextualAction(style: .normal, title: "看过") { action, view, closure in
            closure(true)
        }
        follow.backgroundColor = .systemYellow
        let configuration = UISwipeActionsConfiguration(actions: [fabulous, follow])
        configuration.performsFirstActionWithFullSwipe = false
        return configuration
    }

    var trailingSwipeActionsConfiguration: UISwipeActionsConfiguration {
        let delete = UIContextualAction(style: .destructive, title: "删除") { action, view, closure in
            closure(true)
        }
        return UISwipeActionsConfiguration(actions: [delete])
    }
}

class ViewablePlaceholder: ChromePlaceholder {
    lazy private var avatar: UIImageView = {
        let uiImageView = UIImageView()
        uiImageView.contentMode = .scaleAspectFill
        return uiImageView
    }()

    func add(_ viewable: Viewable, to contentView: UIView) {
        avatar.kf.setImage(with: URL(string: viewable.cover)!)
        contentView.addSubview(avatar)
        avatar.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

    func remove(_ viewable: Viewable, from contentView: UIView) {
        UIView.animate(withDuration: 0.3) {
            self.avatar.alpha = 0.0
        }
    }
}